<?php
class Base extends EngineSupervisor
{
	public $view;
	public $template;
	public $validator;
    public $common;
	
	public function __construct($check=false)
	{
		parent:: __construct();
		include_once(MAIN_PATH.'lib/smarty/libs/Smarty.class.php');
		$smarty =  new Smarty;
		$this -> smarty = $smarty;
		
        $smarty -> template_dir = "public/views/";
        $smarty -> compile_dir  ='public/views_c/'; 
		
		if(!$check)
		{
			$this -> preRender();
		}
		$this -> view = $smarty;
		$this -> template ='';
                $this -> common = new CommonQuery();
		
		$this -> validator = new Validator;
		
	}
	
	public function setActiveMenu($level, $name)
	{
		$this -> view -> assign('menuLv'.$level,$name);
	}
	
	public function setWhere($level, $name, $mod, $view, $act)
	{
		$link = 'index.php?mod='.$mod.'&view='.$view.'&act='.$act;
		$this -> view -> assign('whereLv'.$level,$name);
		$this -> view -> assign('linkLv'.$level,$link);
	}
	
	public function preRender()
	{
		$this-> Authenticate();
	}
	
	private function Authenticate()
	{
		if(!isset($_SESSION[PREFIX]['moderator']['login']))
		{
			$this -> Redirect('back', 'auth', 'loginForm');
		}
	}
    
    protected function makePagination($limit=10,$valueToSmartyName ,$tableName, $what, $where=null, $like=null, $paginationLink=null)
    {
        $offset = 0;
        
        if(isset($_GET['page']) && ctype_digit($_GET['page']))
        {
            $this -> view -> assign('actual_page', $_GET['page']);
            $offset = ($_GET['page'] - 1)*$limit;
        }
        else
        {
            $this -> view -> assign('actual_page', '1');
        }
        
        $news = $this -> common -> select($tableName, $what, $where ,"OFFSET $offset LIMIT $limit",$like);
        $elCount = $this -> common -> select($tableName, "count($what)", $where ,'',$like);
        
        if($paginationLink == null)
        {
            $link = "index.php?"; 
            $check = '1';
            foreach($_GET as $key => $value)
            {
                if($check == '1')
                {                                            
                    $check = '2';
                }
                else
                {
                    $link.="&";
                      
                }
                $link .=$key."=".$value;
            }
        }
        else
        {
            $link = $paginationLink;
        }
        
        $pages = ceil($elCount[0][0]/$limit);
        $this -> view -> assign('paginate', 1);
        $this -> view -> assign($valueToSmartyName, $news); 
        $this -> view -> assign('searchFiltr', $_GET);
        $this -> view -> assign('pagination_link', $link);
        $this -> view -> assign('count_pages', $pages);
    }
}
?>